package com.zackeus.common.utils;

import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Looper;

import androidx.annotation.NonNull;

/**
 * Created by goldze on 2017/5/14.
 * 常用工具类
 */
public final class Utils {

    private static Application context;

    private Utils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    private static void assertInit() {
        if (context == null) {
            throw new NullPointerException("should be initialized in application");
        }
    }

    private static PackageInfo getPackageInfo() {
        assertInit();
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void init(@NonNull final Context context) {
        init((Application) context.getApplicationContext());
    }

    /**
     * 初始化工具类
     * @param application
     */
    public static void init(@NonNull final Application application) {
        if (context != null) {
            throw new IllegalArgumentException(("Can only be initialized once."));
        }

        synchronized (Utils.class) {
            if (context != null) {
                throw new IllegalArgumentException(("Can only be initialized once."));
            }
            context = application;
        }
    }

    /**
     * 获取ApplicationContext
     *
     * @return ApplicationContext
     */
    public static Context getContext() {
        assertInit();
        return context;
    }

    /**
     * 获取应用名称
     * @return
     */
    public static String getAppName() {
        PackageInfo packageInfo = getPackageInfo();
        return packageInfo != null ? packageInfo.applicationInfo.loadLabel(getContext().getPackageManager()).toString() : "";
    }

    /**
     * 获取应用图标
     * @return
     */
    public static Drawable getAppIcon() {
        PackageInfo packageInfo = getPackageInfo();
        return packageInfo != null ? packageInfo.applicationInfo.loadIcon(getContext().getPackageManager()) : null;
    }

    /**
     * 获取应用的VersionCode
     * @return
     */
    public static int getVersionCode() {
        PackageInfo packageInfo = getPackageInfo();
        return packageInfo != null ? packageInfo.versionCode : -1;
    }

    /**
     * 获取应用的VersionName
     * @return
     */
    public static String getVersionName() {
        PackageInfo packageInfo = getPackageInfo();
        return packageInfo != null ? packageInfo.versionName : "";
    }

    /**
     * 是否是主线程
     * @return 是否是主线程
     */
    public static boolean isMainThread() {
        return Looper.getMainLooper() == Looper.myLooper();
    }

    /**
     * 判断当前应用是否处于debug
     * @return
     */
    public static boolean isApkInDebug() {
        try {
            ApplicationInfo info = getContext().getApplicationInfo();
            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (Exception e) {
            return Boolean.FALSE;
        }
    }

    /**
     * 判断是否支持闪光灯
     * @return
     */
    public static boolean hasFlash() {
        return getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }

    /**
     * 获取CPU核心数
     * @return
     */
    public static int getCPUCores() {
        return Runtime.getRuntime().availableProcessors();
    }
}